<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="车系名称" prop="seriesName">
        <el-input v-model="dataForm.seriesName" placeholder="请输入车系名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌名称" prop="brandId">
        <el-select v-model="dataForm.brandId" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in dataForm.brandList"
            :key="item.brandId"
            :label="item.brandName"
            :value="item.brandId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          seriesName: '',
          brandList: [],
          brandId: ''
        },
        dataRule: {
          seriesName: [
            {required: true, message: '车系名称不能为空', trigger: 'blur'}
          ],
          brandId: [
            // eslint-disable-next-line standard/object-curly-even-spacing
            {required: true, message: '请选择品牌名称', trigger: 'change' }
          ]
        }
      }
    },
    methods: {
      change (val) {
        this.$set(this.addForm, 'tableName', val)
        this.$forceUpdate()
      },
      // 查询初始化数据
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/boss/brand/list'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          console.log(data)
          this.dataForm.brandList = data.data
        }).then(() => {
          console.log(this.dataForm.brandList)
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id !== 0) {
            this.$http({
              url: this.$http.adornUrl(`/boss/series/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.dataForm.seriesName = data.data.seriesName
                this.dataForm.brandId = data.data.brandId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/boss/series/save'),
              method: 'post',
              data: this.$http.adornData({
                'seriesId': this.dataForm.id || undefined,
                'seriesName': this.dataForm.seriesName,
                'brandId': this.dataForm.brandId
              })
            }).then(({data}) => {
              if (data && data.code === 1) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
